package de.playmee;

import android.os.SystemClock;
import android.util.Log;

public class Weather {
	
	private final String TAG = "Weather";

	private Condition curweather;
	private int x, y;
	
	private long lastUpdate;
	private long UPDATE_INTERVAL = 1000 * 60 * 5; // 5 min
	
	public enum Condition {
		
		Rainy,
		Cloudy,
		Sunny,
		Snow;
		
		/*
		Sunny	(0, "Sunny", R.drawable.sunny),
		Cloudy	(1, "Cloudy", R.drawable.cloudy),
		Rainy	(2, "Rainy", R.drawable.rainy),
		Snow	(3, "Snow", R.drawable.snow);
		
		private final int id;
		private final String s;
		private final int resId;
		
		private Condition(int id, String s, int resId) {
			this.id = id;
			this.s = s;
			this.resId = resId;
		}
		
		public int getId() {
			return id;
		}
		
		public int getResId() {
			return resId;
		}
		
		@Override
		public String toString() {
			return s;
		}
		*/
		
		/**
		 * returns a string array with all conditions
		 * 
		 * @return array
		 */
		public static String[] getStringList() {
			Condition[] conditions = getArray();
			String[] s = new String[conditions.length];
			for (int i = 0; i < conditions.length; i++) {
				s[i] = conditions[i].toString();
			}
			return s;
		}
		
		/**
		 * returns a condition array with all conditions
		 * 
		 * @return array
		 */
		public static Condition[] getArray() {
			return new Condition[] { Sunny, Cloudy, Rainy, Snow };
		}
		
		/**
		 * return the image (resource id) of a given condition
		 * 
		 * @param condition
		 * @return id or null if no image is available for the condition
		 */
		public static Integer getResId(Condition condition) {
			// TODO change enum to (id, string, drawable)
			if (condition == Sunny)
				return R.drawable.sunny;
			if (condition == Cloudy)
				return R.drawable.cloudy;
			if (condition == Rainy)
				return R.drawable.rainy;
			if (condition == Snow)
				return R.drawable.snow;
			return null;
		}
	}
	
	public Weather() {
		// nothing to do
	}
	
	public Weather(int x, int y) {
		update(x, y);
	}
	
	/**
	 * updates the weather concerning the lastly entered position
	 */
	public void update() {
		Log.i(TAG, "Updating the current weather conditions...");
		
		String link = "http://www.google.com/ig/api?weather=,,,"+x+","+y+"&hl=en";
		String weather = XMLParser.parseWeatherConditions(link);
		
		if (weather.equals("Light Rain")
				|| weather.equals("Rain Showers")
				|| weather.equals("Rain")
				|| weather.equals("Showers")
				|| weather.equals("Scattered Showers")
				|| weather.equals("Freezing Rain")
				|| weather.equals("Freezing Drizzle")
				|| weather.equals("Drizzle")) {
			curweather = Condition.Rainy;
		}
		else if (weather.equals("Snow")
				|| weather.equals("Ice/Snow")
				|| weather.equals("Rain and Snow")) {
			curweather = Condition.Snow;
		}
		else if (weather.equals("Sunny")
				|| weather.equals("Mostly Sunny")
				|| weather.equals("Partly Sunny")) {
			curweather = Condition.Sunny;
		}
		else if (weather.equals("Cloudy")
				|| weather.equals("Overcast")
				|| weather.equals("Fog")
				|| weather.equals("Haze")
				|| weather.equals("Mostly Cloudy")
				|| weather.equals("Partly Cloudy")) {
			curweather = Condition.Cloudy;
		}
		else {
			// TODO unknown condition
			curweather = Condition.Cloudy;
		}
		
		lastUpdate = getCurrentTime();
		Log.i(TAG, "Current weather conditions: " + curweather.toString());
	}
	
	/**
	 * updates the weather concerning the given position
	 * 
	 * @param latitude
	 * @param longitude
	 */
	public void update(int latitude, int longitude) {
		this.x = latitude;
		this.y = longitude;
		update();
	}
	
	/**
	 * @return curweather
	 */
	public Condition getCurrentWeather() {
		return curweather;
	}
	
	/**
	 * sets the current weather conditions
	 * 
	 * @param curweather
	 */
	public void setCurrentWeather(Condition curweather) {
		this.curweather = curweather;
	}
	
	/**
	 * returns the milliseconds since boot, including time spent in sleep
	 * 
	 * @return long
	 */
	private long getCurrentTime() {
		return SystemClock.elapsedRealtime();
	}
	
	/**
	 * @return true if time has come to update the weather conditions, false otherwise
	 */
	public boolean isTimeToUpdate() {
		return lastUpdate == 0 || getCurrentTime() - lastUpdate > UPDATE_INTERVAL;
	}
}
